<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%order}}".
 *
 * @property integer $id
 * @property string $order_sn
 * @property integer $goods_id
 * @property string $price
 * @property integer $user_id
 * @property integer $stauts
 * @property integer $created_time
 * @property integer $pay_time
 *
 * @property Goods $goods
 * @property PayLog  $log
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%order}}';
    }
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_time'],
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_sn', 'goods_id', 'price', 'user_id'], 'required'],
            [['goods_id', 'user_id', 'stauts', 'created_time', 'pay_time'], 'integer'],
            [['price'], 'number'],
            [['order_sn'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_sn' => 'Order Sn',
            'goods_id' => 'Goods ID',
            'price' => 'Price',
            'user_id' => 'User ID',
            'stauts' => 'Stauts',
            'created_time' => 'Created Time',
            'pay_time' => 'Pay Time',
        ];
    }
    
    public function getLog()
    {
        return $this->hasOne(PayLog::className(), ['order_sn' => 'order_sn']);
    }
    
    public function getGoods()
    {
        return $this->hasOne(Goods::className(), ['goods_id' => 'goods_id']);
    }
    
    public static function findByOrderSn($order_sn)
    {
        return static::findOne(['order_sn' => $order_sn]);
    }
    
    public static function findById($id)
    {
        return static::findOne(['id' => $id]);
    }
    
}
